package org.boot.easyapi.core.framework.web;

import lombok.extern.slf4j.Slf4j;
import org.boot.easyapi.pojo.web.CommonResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static org.boot.easyapi.pojo.web.CommonResult.error;


/**
 * 全局异常监听
 */
@Slf4j
@RestControllerAdvice   // 只拦截 Controller 层抛出的异常
public class GlobalExceptionListener {

    /**
     * 只监听 RuntimeException（含所有子类）
     */
    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<CommonResult<Object>> handleRuntime(RuntimeException ex) {
        String message = ex.getMessage();
        log.error("RuntimeException: {}", message, ex);
        CommonResult<Object> error = error(500, message);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
    }
}